package com.hnkywl.sts.dao.tjbb;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:14
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ZjzyDao extends SimpleHibernateTemplate {
    /**
     * 财务收发存报表
     *
     * @param gsid
     * @param
     * @param
     * @return
     */
    public Map<String, List> zjzyList(Integer gsid, Date enddt, Integer type, Integer tc_flag) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(enddt);
       // calendar.add(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date paramDate = new java.sql.Date(calendar.getTime().getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_ZJZY(?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setDate(2, paramDate);
            call.setInt(3, type);
            call.setInt(4, tc_flag);
            call.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(5);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("jc").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }

    /**
     * 库存金额
     *
     * @param gsid
     * @param type
     * @param enddt
     * @return
     */
    public Map<String, Object> bgkcjeList(Integer gsid, Integer type, Date enddt, Integer tc_flag, Integer pageNo, Integer pageSize) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(enddt);
        //calendar.add(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date paramDate = new java.sql.Date(calendar.getTime().getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_ZJZY_KCZJMX(?,?,?,?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setDate(2, paramDate);
            call.setInt(3, type);
            call.setInt(4, tc_flag);
            call.setInt(5, (pageNo - 1) * pageSize);
            call.setInt(6, pageNo * pageSize);
            call.registerOutParameter(7, Types.NUMERIC);
            call.registerOutParameter(8, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            total = call.getInt(7);
            rs = ((OracleCallableStatement) call).getCursor(8);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("cggs").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    /**
     * 在途资金
     *
     * @param gsid
     * @param type
     * @param enddt
     * @param tc_flag
     * @return
     */
    public List<Map> findbqztzj(Integer gsid, Integer type, Date enddt, Integer tc_flag) {
        String sql = "  select a.cgdh,to_char(a.dt,'yyyy-mm-dd') cgrq,a.cggs,a.gys,a.code, a.wz_name,a.ggxh,a.th,a.sl,a.hsj,a.je from V_RPT_ZJZY_ZTZJ a where gs_id = :gsid and type = :type  and dt <= :enddt  and tc_flag = :tc_flag " +
                " union all " +
                "select '合计',null,null,null,null, null,null,null,sum(a.sl),null, sum(a.je) from V_RPT_ZJZY_ZTZJ a where gs_id = :gsid and type = :type and dt <= :enddt and tc_flag = :tc_flag ";
        SQLQuery sqlQuery = createSQLQuery(sql);
        sqlQuery.setParameter("gsid", gsid);
        sqlQuery.setParameter("type", type);
        sqlQuery.setParameter("enddt", enddt);
        sqlQuery.setParameter("tc_flag", tc_flag);
        List<Map> result = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        return result;
    }

    //付款
    public Map<String, List> bgyufkList(Integer gsid, Date enddt, Integer type, Integer tc_flag) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(enddt);
        //calendar.add(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date paramDate = new java.sql.Date(calendar.getTime().getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_ZJZY_FKMX(?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setDate(2, paramDate);
            call.setInt(3, type);
            call.setInt(4, tc_flag);
            call.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(5);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("gs_name").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    //收款
    public Map<String, List> bgyingskList(Integer gsid, Date enddt, Integer type, Integer tc_flag) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(enddt);
        //calendar.add(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date paramDate = new java.sql.Date(calendar.getTime().getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_ZJZY_SKMX(?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setDate(2, paramDate);
            call.setInt(3, type);
            call.setInt(4, tc_flag);
            call.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(5);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("gs_name").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }
}
